#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : lee

import sys
import os
import logging
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    import pymel.core as pm
    import jarryLibs.mayakit.MayaUtils as mu
    reload(mu)
except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CheckPgYetiMayaNodesType(CheckBase):
    def __init__(self):
        super(CheckPgYetiMayaNodesType, self).__init__()
        pass

    def check_pgYetiMaya_subnodes(self):
        miss_groups = []
        top_group_name = "cfx_grp"
        if not cmds.objExists(top_group_name):
            miss_groups.append(top_group_name)
            return

        pgYetiMaya_nodes = self.get_pgYetiMaya_nodes()
        error_type_node = []
        for node in pgYetiMaya_nodes:
            if cmds.listRelatives(node, s=True, c=True):
                for shape_node in cmds.listRelatives(node, s=True, c=True):
                    if cmds.nodeType(shape_node) != "pgYetiMaya":
                        error_type_node.append(node)
            else:
                if cmds.nodeType(node) != "scaleConstraint" and cmds.nodeType(node) != "parentConstraint":
                    error_type_node.append(node)

        return error_type_node

    def get_pgYetiMaya_nodes(self):
        # 通过文件名称获取资产信息
        maya_file = mu.sceneName()
        file_name = os.path.basename(maya_file)
        asset_short_type = file_name.split("_")[0]
        asset_name = file_name.split("_")[1]

        pgYetiMaya_nodes = []
        groups_type = ["pgYetiMaya_dyn", "pgYetiMaya_static"]
        for type in groups_type:
            pgYetiMaya_group = "{0}_{1}_{2}_grp".format(asset_short_type, asset_name, type)
            if cmds.objExists(pgYetiMaya_group):
                for node in cmds.listRelatives(pgYetiMaya_group, c=True):
                    pgYetiMaya_nodes.append(node)

        return pgYetiMaya_nodes

    def check(self):
        result_proc = self.check_pgYetiMaya_subnodes()
        if result_proc:
            result_log = u'Error:%s\n' % result_proc
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

    def fix(self):
        pass
